package com.taobao.wolfwolf.web.servler;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;

import com.taobao.wolfwolf.hibernate.pojo.Contact;
import com.taobao.wolfwolf.hibernate.pojo.Express;
import com.taobao.wolfwolf.hibernate.pojo.Expressfieldposition;
import com.taobao.wolfwolf.hibernate.service.ExpressService;

/**
 * <p>快递单管理类</p>
 * @author qinanhg
 * 创建日期: 2012-4-25 下午12:08:04
 */
public class ExpressManager extends HttpServlet {

	private static final long serialVersionUID = 8385847624251398446L;
	
	private ExpressService eservice;

	public ExpressManager() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf8");
		response.setContentType("text/html");
		
		PrintWriter out = response.getWriter();
		
		ApplicationContext context = org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		eservice = (ExpressService) context.getBean("ExpressService");
		
		String type = request.getParameter("type");
		
		if(type.equals("add")) {
			addExpress(request);
		}
		
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}
	
	private void addExpress(HttpServletRequest request) {
		// base info
		String express_name = request.getParameter("express_name");
		String express_codename = request.getParameter("express_codename");
		String first_weight = request.getParameter("first_weight");
		String second_weight = request.getParameter("second_weight");
		
		// contact
		String contact_name = request.getParameter("contact_name");
		String contact_phone = request.getParameter("contact_phone");
		String contact_mobile = request.getParameter("contact_mobile");
		
		// express postion
		int width = Integer.valueOf(request.getParameter("width"));
		int height = Integer.valueOf(request.getParameter("height"));
		String postion_sender_name = request.getParameter("postion_sender_name");
		String postion_source_city = request.getParameter("postion_source_city");
		String postion_company_name = request.getParameter("postion_company_name");
		String postion_sender_address_1 = request.getParameter("postion_sender_address_1");
		String postion_sender_address_2 = request.getParameter("postion_sender_address_2");
		String postion_seller_phone = request.getParameter("postion_seller_phone");
		String postion_sell_goods = request.getParameter("postion_sell_goods");
		String postion_seller_sign = request.getParameter("postion_seller_sign");
		String postion_year = request.getParameter("postion_year");
		String postion_month = request.getParameter("postion_month");
		String postion_day = request.getParameter("postion_day");
		String postion_receiver_name = request.getParameter("postion_receiver_name");
		String postion_destination_city = request.getParameter("postion_destination_city");
		String postion_receiver_company = request.getParameter("postion_receiver_company");
		String postion_receiver_address1 = request.getParameter("postion_receiver_address1");
		String postion_receiver_address2 = request.getParameter("postion_receiver_address2");
		String postion_buyer_phone = request.getParameter("postion_buyer_phone");
		
		Express express = new Express();
		express.setName(express_name);
		express.setCodeName(express_codename);
		if(!first_weight.equals(""))
			express.setFirstWeightPrice(Long.valueOf(first_weight));
		if(!second_weight.equals(""))
			express.setSecondWeightPrice(Long.valueOf(second_weight));
		Contact cnt = null;
		if(!contact_name.equals("")) {
			cnt = new Contact();
			cnt.setName(contact_name);
			cnt.setMobile(contact_mobile);
			cnt.setPhone(contact_phone);
		}
		
		Expressfieldposition position = new Expressfieldposition();
		
		position.setWidth(width);
		position.setHeight(height);
		
		if(!isEmpty(postion_sender_name))
			position.setSenderName(postion_sender_name);
		
		if(!isEmpty(postion_source_city))
			position.setSourceCity(postion_source_city);
		
		if(!isEmpty(postion_company_name))
			position.setCompanyName(postion_company_name);
		
		if(!isEmpty(postion_sender_address_1))
			position.setSenderAddress1(postion_sender_address_1);
		
		if(!isEmpty(postion_sender_address_2))
			position.setSenderAddress2(postion_sender_address_2);
		
		if(!isEmpty(postion_seller_phone))
			position.setSellerPhone(postion_seller_phone);
		
		if(!isEmpty(postion_sell_goods))
			position.setSellGoods(postion_sell_goods);
		
		if(!isEmpty(postion_seller_sign))
			position.setSellerSign(postion_seller_sign);
		
		if(!isEmpty(postion_year))
			position.setYear(postion_year);
		
		if(!isEmpty(postion_month))
			position.setMonth(postion_month);
		
		if(!isEmpty(postion_day))
			position.setDay(postion_day);
		
		if(!isEmpty(postion_receiver_name))
			position.setReceiverName(postion_receiver_name);
		
		if(!isEmpty(postion_destination_city))
			position.setDestinationCity(postion_destination_city);
		
		if(!isEmpty(postion_receiver_company))
			position.setReceiverCompany(postion_receiver_company);
		
		if(!isEmpty(postion_receiver_address1))
			position.setReceiverAddress1(postion_receiver_address1);
		
		if(!isEmpty(postion_receiver_address2))
			position.setReceiverAddress2(postion_receiver_address2);
		
		if(!isEmpty(postion_buyer_phone))
			position.setBuyerPhone(postion_buyer_phone);
		
		if(cnt != null)
			express.setContact(cnt);
		express.setExpressfieldposition(position);
		
		eservice.create(express);
	}
	
	private boolean isEmpty(String str) {
		if(str.equals(""))
			return true;
		return false;
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
